package com.example.zhang;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.HashMap;

public class ThirdFragment extends Fragment implements  AdapterView.OnItemLongClickListener {
    ListView list;
    private final String TAG ="a";
    private SimpleAdapter adapter;
    private ArrayList<HashMap<String, String>> listItems;
    private FloatingActionButton fb;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_third_fragment,null);
        listItems = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map= new HashMap<String, String>();
        list = (ListView)view.findViewById(R.id.mi);
        DBManager  db =new DBManager(getActivity());
        if(db.listAll_t().isEmpty())//判断数据库是否有表
            Log.i(TAG,"数据库收藏为空");
        else {
            listItems = db.listAll_t();
            adapter = new SimpleAdapter(getActivity(), listItems, R.layout.list_item,
                    new String[]{"ItemTitle", "ItemDetail"}, new int[]{R.id.textView3, R.id.textView4});
            list.setAdapter(adapter);

        }
        list.setOnItemLongClickListener(this);

        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstancesState){
        super.onActivityCreated(savedInstancesState);
        fb = getActivity().findViewById(R.id.floatingActionButton);
        fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //点击按钮更新当前页面listview
                DBManager  db =new DBManager(getActivity());
                if(db.listAll_t().isEmpty())//判断数据库是否有表
                    Log.i(TAG,"数据库收藏为空");
                else {
                    list.setAdapter(null);
                    listItems = db.listAll_t();
                    adapter = new SimpleAdapter(getActivity(), listItems, R.layout.list_item,
                            new String[]{"ItemTitle", "ItemDetail"}, new int[]{R.id.textView3, R.id.textView4});
                    list.setAdapter(adapter);

                }
            }
        });
    }
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position,long id){ //长按删除
        Log.i(TAG, "In onItemLongClick...");
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示")
                .setMessage("请确认是否删除")
                .setNegativeButton("否",null)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listItems.remove(position);
                        adapter.notifyDataSetChanged();
                        DBManager  db =new DBManager(getActivity());
                        db.deleteAll_t(); //更新数据库
                        db.addAll_t(listItems);
                    }
                });
        builder.create().show();
        return true;
    }

}